package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.io.File;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;
import me.shaohui.advancedluban.OnCompressListener;

/**
 * Created by U城网 on 2017/6/8.
 * 个人信息界面
 */

public class MyPerInfoActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private RelativeLayout head_rl,  home_rl, emotion_rl;
    private TextView per_name, per_sex, per_id, per_home, per_emotion, per_sign, per_school, per_realName;
    private ImageView per_head;
    private String headUrl;//上传成功头像
    private String headUrlGet;//获取到的头像
    private LinearLayout sex_linear, name_rl, sign_rl;
    private TextView perInfo_year;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_per_info);
        setTitle("编辑");
//        setRightText("完成", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

        initView();
    }

    private void initView (){
        per_head = (ImageView) findViewById(R.id.perInfo_head);
        per_head.setOnClickListener(this);

//        head_rl = (RelativeLayout) findViewById(R.id.perInfo_head_rl);
//
        name_rl = (LinearLayout) findViewById(R.id.perInfo_name_rl);

//        sex_linear = (LinearLayout) findViewById(R.id.perInfo_sex_linear);

//        home_rl = (RelativeLayout) findViewById(R.id.perInfo_home_rl);

//        emotion_rl = (RelativeLayout) findViewById(R.id.perInfo_emotion_rl);

        sign_rl = (LinearLayout) findViewById(R.id.perInfo_sign_rl);

        per_name = (TextView) findViewById(R.id.perInfo_name);
        per_sex = (TextView) findViewById(R.id.perInfo_sex);
//        per_id = (TextView) findViewById(R.id.perInfo_id);
//        per_home = (TextView) findViewById(R.id.perInfo_home);
//        per_emotion = (TextView) findViewById(R.id.perInfo_emotion);
        per_sign = (TextView) findViewById(R.id.perInfo_sign);
        per_school = (TextView) findViewById(R.id.perInfo_school);
        perInfo_year = (TextView) findViewById(R.id.perInfo_year);
//        per_realName = (TextView) findViewById(R.id.perInfo_realName);

        switch (UChengApplication.userRole){
            case BaseConfig.STUDENT_ROLE:
//                head_rl.setOnClickListener(this);
                name_rl.setOnClickListener(this);
//                home_rl.setOnClickListener(this);
//                emotion_rl.setOnClickListener(this);
                sign_rl.setOnClickListener(this);
                break;
            case BaseConfig.UNI_ROLE:
            case BaseConfig.EDITOR_ROLE:
            case BaseConfig.ZONG_ROLE:
                name_rl.setOnClickListener(this);
//                sex_linear.setVisibility(View.GONE);
                break;
        }

        getMyInfo();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseConfig.isRelease){
            getMyInfo();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        BaseConfig.isRelease = false;
    }

    /**
     * 获取个人信息
     * */
    private void getMyInfo(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, UCommonUtil.getUtil().getId());
        HttpManager.newInstance(this).query(UserConfig.ChangeInfo.method_check, UserConfig.ChangeInfo.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                UCommonUtil.getUtil().showToast(MyPerInfoActivity.this, msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status == 0){
                    if (TextUtils.isEmpty(data)){
                        UCommonUtil.getUtil().showToast(MyPerInfoActivity.this, "获取个人信息失败");
                        return;
                    }

                    UserBean userBean = JsonUtils.getManager().stringToBean(data, UserBean.class);
                    headUrlGet = userBean.getUserAvatar();
                    if (TextUtils.isEmpty(headUrlGet)){
                        ImageManager.getInstance().loadImage(MyPerInfoActivity.this, per_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                    }else {
                        ImageManager.getInstance().loadImage(MyPerInfoActivity.this, per_head, headUrlGet, ImageManager.ImageType.CIRCLE);
                    }


                    per_name.setText(TextUtils.isEmpty(userBean.getNickName()) ? "暂无" : userBean.getNickName());
                    per_sex.setText(userBean.getSex()== 0 ? "女" : "男");
//                    per_id.setText(userBean.getId()+"");
//                    if (!TextUtils.isEmpty(userBean.getHometown())){
//                        per_home.setText(userBean.getHometown());
//                    }else {
//                        per_home.setHint("未设置");
//                    }
//                    if (!TextUtils.isEmpty(userBean.getEmotionState())){
//                        per_emotion.setText(userBean.getEmotionState());
//                    }else {
//                        per_emotion.setHint("未设置");
//                    }
                    if (!TextUtils.isEmpty(userBean.getUserSign())){
                        per_sign.setText(userBean.getUserSign());
                    }else {
                        per_sign.setHint("未设置");
                    }
                    if (!TextUtils.isEmpty(userBean.getUniName())){
                        per_school.setText(userBean.getUniName());
                    }else {
                        per_school.setHint("未设置");
                    }
//                    if (!TextUtils.isEmpty(userBean.getRealName())){
//                        per_realName.setText(userBean.getRealName());
//                    }else {
//                        per_realName.setHint("未设置");
//                    }
                }else {
                    UCommonUtil.getUtil().showToast(MyPerInfoActivity.this, msg);
                }
            }
        });

    }


    /**
     * 选择图片
     * */
    private void chooseImage(){
        UCommonUtil.getUtil().openAndCrop(this, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                Log.d("jjf", "裁剪的地址="+data);
                String headUrl = data;
                if (!TextUtils.isEmpty(headUrl)){
                    ImageManager.getInstance().loadImage(MyPerInfoActivity.this, per_head, headUrl, ImageManager.ImageType.CIRCLE);
                    compress(headUrl);
                }
            }
        });
    }


    /**
     * 压缩图片在上传
     * */
    private void compress(String path){
        LoadingUtils.getUtils().loading(this);
        UCommonUtil.getUtil().compressImageAlone(this, 300, path, new OnCompressListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(File file) {
                String head = file.getAbsolutePath();//压缩后的想地址
                Log.d("jjf", "上传时候的地址="+head);

                //上传到阿里云
                OSSManager.getOSSManager().putHeadToOss(head, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        LoadingUtils.getUtils().hide();
                        UCommonUtil.getUtil().showToast(MyPerInfoActivity.this, "图片上传失败");
                        if (TextUtils.isEmpty(headUrlGet)){
                            ImageManager.getInstance().loadImage(MyPerInfoActivity.this, per_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                        }else {
                            ImageManager.getInstance().loadImage(MyPerInfoActivity.this, per_head, headUrlGet, ImageManager.ImageType.CIRCLE);
                        }
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        //上传成功返回的oss的头像地址
                        headUrl = data;
                        MapRequest map = new MapRequest();
                        map.put(BaseConfig.USER_AVATAR, data);
                        map.put(BaseConfig.ID, UCommonUtil.getUtil().getId());
                        putInfo(map);
                    }
                });

            }

            @Override
            public void onError(Throwable e) {
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(MyPerInfoActivity.this, "图片选择错误");
            }
        });
    }

    /**
     * 将信息提交到我们自己的服务器
     * */

    private void putInfo(MapRequest map){
        HttpUserManager.getManager().changeUserInfo(this, map, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (stutas ==0 ){
                    UCommonUtil.getUtil().showToast(MyPerInfoActivity.this, "修改成功");
                    UChengApplication.userAvatar = headUrl;
                    SPUtils.getPreferences(MyPerInfoActivity.this).putString(BaseConfig.USER_AVATAR, headUrl);

                    //同步更新 防止聊天界面 头像不更新
                    UserInfo userInfo = new UserInfo(String.valueOf(UCommonUtil.getUtil().getId()), UChengApplication.nickName, Uri.parse(UChengApplication.userAvatar));
                    RongIM.getInstance().setCurrentUserInfo(userInfo);//对方会收到当前修改的信息
                    RongIM.getInstance().refreshUserInfoCache(userInfo);//聊天界面自己发送的消息 会显示当前修改的
                    return;
                }
                UCommonUtil.getUtil().showToast(MyPerInfoActivity.this, "修改失败");
                if (TextUtils.isEmpty(headUrlGet)){
                    ImageManager.getInstance().loadImage(MyPerInfoActivity.this, per_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                }else {
                    ImageManager.getInstance().loadImage(MyPerInfoActivity.this, per_head, headUrlGet, ImageManager.ImageType.CIRCLE);
                }
            }
        });
    }




    @Override
    public void onClick(View view) {
        Intent intent = null;

        switch (view.getId()){
            case R.id.perInfo_head:
                chooseImage();
                break;
            case R.id.perInfo_name_rl:
                intent = new Intent(this, EditorInfoActivity.class);
                intent.putExtra(BaseConfig.TYPE_STRING, per_name.getText().toString());
                intent.putExtra(BaseConfig.TYPE, 1);

                break;
//            case R.id.perInfo_home_rl://家乡
//                intent = new Intent(this, EditorInfoActivity.class);
//                intent.putExtra(BaseConfig.TYPE_STRING, per_home.getText().toString());
//                intent.putExtra(BaseConfig.TYPE, 3);
//                break;
//            case R.id.perInfo_emotion_rl://感情状态
//                intent = new Intent(this, EditorInfoActivity.class);
//                intent.putExtra(BaseConfig.TYPE_STRING, per_emotion.getText().toString());
//                intent.putExtra(BaseConfig.TYPE, 4);
//                break;
            case R.id.perInfo_sign_rl:
                intent = new Intent(this, EditorInfoActivity.class);
                intent.putExtra(BaseConfig.TYPE_STRING, per_sign.getText().toString());
                intent.putExtra(BaseConfig.TYPE, 2);
                break;
        }
        if (intent != null){
            startActivity(intent);
        }
    }
}
